package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import java.util.List;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/10/23 15:58
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Reference
    UserService userService;

    // 获取用户信息（用户名）
    @RequestMapping(value = "/getUsername")
    public Result getUsername(){
        // 获取用户信息（从SpringSecurity）
        try {
            User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,username);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }

    }
    //修改用户密码功能：
    @RequestMapping(path = "/updatePassword")
    public Result updatePassword(@RequestBody Map map){
        //1.接受参数
        try{
            userService.updatePassword(map);
            return new Result(true,"恭喜你修改密码成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改密码失败");
        }
    }


    // 用户列表显示
    @RequestMapping(value = "/pageQuery")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = userService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString()
        );
        return pageResult;
    }

    @RequestMapping(value = "findRoleList")
    public Result findRoleList() {
        try {
            List<Role> role = userService.findRoleList();
            return new Result(true, "查询角色成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色失败");
        }
    }

    @RequestMapping(value = "/add")
    public Result add(@RequestBody com.itheima.health.pojo.User user, Integer[] roleIds) {
        try {
            userService.add(user, roleIds);
            return new Result(true, "添加用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加用户失败");
        }
    }

    @RequestMapping(value = "/findById")
    public Result findById(Integer id) {
        try {
            com.itheima.health.pojo.User user = userService.findById(id);
            return new Result(true, "用户查询成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "用户查询失败");
        }

    }

    @RequestMapping(value = "/findroleIds")
    public List<Integer> findroleIds(Integer id) {
        try {
            List<Integer> roleIds = userService.findroleIds(id);
            return roleIds;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value = "/edit")
    public Result edit(@RequestBody com.itheima.health.pojo.User user, Integer[] roleIds) {
        try {
            userService.edit(user, roleIds);
            return new Result(true, "编辑用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑用户失败");
        }
    }

    @RequestMapping(value = "/delete")
    public Result delete(Integer id) {
        try {
            userService.delete(id);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除用户失败");
        }
        return new Result(true, "删除用户成功");
    }

}
